<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form size="mini" :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
    <el-form-item label="表名" prop="tableCode">
      <el-input clearable v-model="dataForm.tableCode" placeholder="表名"></el-input>
    </el-form-item>
    <el-form-item label="前缀" prop="prefix">
      <el-input clearable v-model="dataForm.prefix" placeholder="前缀"></el-input>
    </el-form-item>
    <el-form-item label="日期串" prop="dateStr">
      <el-input clearable v-model="dataForm.dateStr" placeholder="日期串"></el-input>
    </el-form-item>
    <el-form-item label="当前值" prop="lastValue">
      <el-input clearable v-model="dataForm.lastValue" placeholder="当前值"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input clearable v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">取消</el-button>
      <el-button size="mini" type="primary" @click="dataFormSubmit()" :loading="btnLoading">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        btnLoading: false,
        dataForm: {
          id: 0,
          tableCode: '',
          prefix: '',
          dateStr: '',
          lastValue: '',
          remark: ''
        },
        dataRule: {
          tableCode: [
            { required: true, message: '表名不能为空', trigger: 'blur' }
          ],
          prefix: [
            { required: true, message: '前缀不能为空', trigger: 'blur' }
          ],
          dateStr: [
            { required: true, message: '日期串不能为空', trigger: 'blur' }
          ],
          lastValue: [
            { required: true, message: '当前值不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.requestUrl(`/sys/codeinstance/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.requestParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.tableCode = data.info.tableCode
                this.dataForm.prefix = data.info.prefix
                this.dataForm.dateStr = data.info.dateStr
                this.dataForm.lastValue = data.info.lastValue
                this.dataForm.remark = data.info.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.btnLoading = true
            this.$http({
              url: this.$http.requestUrl(`/sys/codeinstance/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.requestData({
                'id': this.dataForm.id || undefined,
                'tableCode': this.dataForm.tableCode,
                'prefix': this.dataForm.prefix,
                'dateStr': this.dataForm.dateStr,
                'lastValue': this.dataForm.lastValue,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
              this.btnLoading = false
            })
          }
        })
      }
    }
  }
</script>
